package menu.lt.pers.menu.dao;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import menu.lt.pers.menu.Bean.RecipeBean;
import menu.lt.pers.menu.Bean.UserBean;
import java.text.SimpleDateFormat;
import java.util.Date;

public class HistoryDao {
    private DBHelper helper;
    private SQLiteDatabase db;
    private SimpleDateFormat format;

    public HistoryDao(Context context) {
        helper = new DBHelper(context,Dbdata.dbname,null,Dbdata.version);
        db = helper.getReadableDatabase();
        format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    }
    //数据库插入操作
    public void insert(RecipeBean recipe){
        String sql="insert into "+Dbdata.historytable+" ("+
                Dbdata.historyColumn[1]+","+
                Dbdata.historyColumn[2]+","+Dbdata.historyColumn[3]+","+Dbdata.historyColumn[4]+","+
                Dbdata.historyColumn[5]+","+Dbdata.historyColumn[6]+","+Dbdata.historyColumn[7]+","+
                Dbdata.historyColumn[8]+","+Dbdata.historyColumn[9]+","+Dbdata.historyColumn[10]+","+
                Dbdata.historyColumn[11]+","+Dbdata.historyColumn[12]+
                ")values(?,?,?,?,?,?,?,?,?,?,?,?)";
        db.execSQL(sql,
                new String[]{
                        String.valueOf(recipe.getClassid()),
                        recipe.getName(),
                        recipe.getPeoplenum(),
                        recipe.getPreparetime(),
                        recipe.getCookingtime(),
                        recipe.getContent(),
                        recipe.getPic(),
                        recipe.getTag(),
                        recipe.getMaterialJson(),
                        recipe.getProcessJson(),
                        String.valueOf(recipe.getUserid()),
                        format.format(new Date())
                });
    }
    public Cursor query(){
        Cursor cursor=db.query(Dbdata.historytable,null,null,null,null,null,null);
        return cursor;
    }

    public Cursor findByUserId(UserBean userBean){
        int id = userBean.getId();
        String selection = Dbdata.historyColumn[11]+" = ?";
        String[] selectionArgs = new String[]{String.valueOf(id)};
        Cursor cursor=db.query(Dbdata.historytable,null,selection,selectionArgs,null,null,null);
        return cursor;
    }

    public void delete(){
        String sql = "delete from " + Dbdata.historytable + " where _id > 0";
        db.execSQL(sql);
    }
}
